package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-29 12:01
 **/
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Order add(Map map) {
        //姓名, 手机号, 性别, 身份证号码, 体检日期
        String telephone = (String)(map.get("telephone"));
        Integer setmealId = Integer.parseInt((String)map.get("setmealId"));
        String orderType = (String)(map.get("orderType"));
        String name = (String)(map.get("name"));
        String sex = (String)(map.get("sex"));
        String idCard = (String)(map.get("idCard"));
        Date orderDate = null;
        try {
            orderDate = DateUtils.parseString2Date((String)map.get("orderDate"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //1.首先根据身份证号码和手机号,判断是不是会员,不是会员添加会员,并返回ID
        Member member = orderMapper.queryMember(telephone);
        if (member == null) {
            member = new Member();
            member.setIdCard(idCard);
            member.setName(name);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            member.setRegTime(new Date());
            member.setIdCard(idCard);
            orderMapper.addMember(member);
        }
        //2.根据预约日期查询是否可预约
        OrderSetting orderSetting = orderMapper.queryOrderSettingByDate(orderDate);
        if (orderSetting == null) {
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //3.根据预约日期查询预约是否满了
        if (orderSetting.getReservations() == orderSetting.getNumber()) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }
        //5.根据套餐ID,预约日期,会员ID判断是否已经预约,预约返回抛出异常
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(setmealId);
        Order order1 = orderMapper.queryOrder(order);
        if (order1 != null) {
            throw new RuntimeException(MessageConstant.HAS_ORDERED);
        }
        //6.添加预约,并返回订单信息
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderMapper.addOrder(order);
        //7.预约成功后在预约列表添加一个人数
        orderSettingMapper.addReservationsByDate(orderDate);
        return order;
    }

    @Override
    public Map orderInfo(Integer id) {

        return orderMapper.orderInfo(id);
    }
}
